index_app.controller("comm_detailed_list", function ($scope,$http) {

	var grid_selector = "#grid-table";
	var pager_selector = "#grid-pager";


	$(window).on('resize.jqGrid', function() {
		$(grid_selector).jqGrid('setGridWidth', $(".page-content").width());
	});

	jQuery(grid_selector).jqGrid('aceInit', pager_selector, {
		url:"commodityDetailed/listDetailed",
		datatype : "json",
		colNames : ['','ID', '商品编号','商品名称','商品单价','所属类别', '管理操作'],
		colModel : [
		            {name:'myac',hidden:true},
		            {name:'detailedKey',index:'detailedKey', width:60,hidden:true},
		            {name:'sku',index:'sku', width:60},
		             {name:'name',index:'name', width:60},
		                {name:'price',index:'price', width:60},
		              {name:'skuType.skuType',index:'skuType', width:60},
		           {name:'detailedKey',index:'detailedKey',
                   		  width:60, sorttype:"integer",
                   		    formatter: function(value,row,index){
                   		        return "<a style='cursor:pointer' onclick='updateDetailed("+index.detailedKey+")'>修改</a> || <a style='cursor:pointer' onclick='deleteDetailed("+index.detailedKey+")'>删除</a>";
                   		    }
                   	}
		            ],
		            sortable:true,
		            sortname:'detailedKey',
		            sortorder:'asc',
		            caption : "<i class='menu-icon fa fa-male white'></i> 商品设置"
	});
	jQuery(grid_selector).jqGrid('aceNavGrid', pager_selector);
	//});

    $scope.commTypeAdd = function(){
         $("#commTypeAdd").attr("href","#comm_detailed_edit/null");
    }

	$scope.commodityTypeUpdate = function(commodityType){
		$http({
			method  : 'POST',
			url     : 'commodityType/insertAll',
			params    : commodityType,//传递真个对象到后台
			headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
		}).success(function(data) {
			if(data == 1){
				//添加保存标题样式
				$scope.title = '<h4 class="easyDialog_title" id="easyDialogTitle"><i class="icon-ok green"></i> <a href="javascript:void(0)" title="关闭窗口" class="close_btn" id="closeBtn">&times;</a>保存</h4>';
				//保存成功内容
				$scope.toolTip = "保存成功";
				//弹窗
				toolTipDialog();
				$scope.success = true;
                $('#myModal').modal('hide');
			} else {
				$scope.title = "保存";
				$scope.toolTip = "保存失败";
				toolTipDialog();
			}
		});
	}
	function toolTipDialog(){
    		easyDialog.open({
    			container : {
    				header : $scope.title,
    				content : $scope.toolTip,
    				yesFn : $scope.toolFn,
    				noFn : false
    			},
    			fixed : false
    		});
    	}
    	//弹窗中确定按钮事件
    	$scope.toolFn = function(){
    		if( $scope.success){//成功
    			window.location.href="#comm_detailed_list";
    		}
    		return true;
    	}

})
var deleteKey = 0;
function deleteDetailed(key){
	    deleteKey = key;
        console.log(deleteKey);
	    deleteTipDialog();
}

function updateDetailed(key){
     window.location.href="#comm_detailed_edit/"+key;
}

//修改提示
    	function deleteTipDialog(){
    		easyDialog.open({
    			container : {
    				header : '<h4 class="easyDialog_title" id="easyDialogTitle"><i class="icon-ok green"></i> <a href="javascript:void(0)" title="关闭窗口" class="close_btn" id="closeBtn">&times;</a>修改提示</h4>',
    				content : "是否删除该商品",
    				yesFn : modifyFn,
    				noFn : modifyNo
    			},
    			fixed : false
    		});
    	}

    	 function modifyNo(){
                 window.location.href="#comm_detailed_list";
           		return true;
          };
           function modifyFn(){
           var toolTip = "";
           $.ajax({
           			method  : 'POST',
           			url     : 'commodityDetailed/deleteByKey?detailedKey='+ deleteKey,
           		}).success(function(data) {
           			if(data == 1){
           				//删除提示内容
           				toolTip = "删除成功";
           				//弹窗
           				toolTipDialog(toolTip);
           			} else {
           				//删除提示内容
           				$scope.toolTip = "删除失败,该商品被引用";
           				//弹窗
           				toolTipDialog(toolTip);

           			}
           		});
           		return true;
           	};
    function toolTipDialog(toolTip){
    		easyDialog.open({
    			container : {
    				header : "删除",
    				content : toolTip,
    				yesFn :toolFn,
    				noFn : false
    			},
    			fixed : false
    		});
    	}
    	//弹窗中确定按钮事件
    	function toolFn(){
    		window.location.href="#comm_detailed_list";
    		return true;
    	}